import { reqCartlist, reqCartadd, reqCartedit, reqCartdel } from "../../request/api"
//初始值
const initState = {
    //购物车列表
    Cartlist: [],
    filter: 0,
    num: 1,
    type: 1,
    allChecked: false,
    isreq: true
}

//actionTypes
const types = {
    //修改 
    CHANGE_CART_LIST: "SHOP_CHANGE_CART_LIST",
    CHANGE_FILTER: "SHOP_CHANGE_FILTER",
    CHANGE_ALL: "SHOP_CHANGE_ALL",
    CHANGE_PRICE: "SHOP_CHANGE_PRICE",
    CHANGE_ISREQ: "SHOP_CHANGE_ISREQ"
}

//reducer 修改数据
const reducer = (state = initState, action) => {
    switch (action.type) {
        case types.CHANGE_CART_LIST:
            return {
                ...state,
                Cartlist: action.arr,

            }
        case types.CHANGE_ALL:
            return {
                ...state,
                allChecked: action.e,
                Cartlist: state.Cartlist.map(item => {
                    return {
                        ...item,
                        checked: action.e
                    }
                })
            }
        case types.CHANGE_ISREQ:
            return {
                ...state,
                isreq: action.isreq
            }
        default:
            return state;
    }

}

//actionCreators组件派发任务
export const actions = {
    //修改，请求
    //
    //修改isreq
    changeIsreq: isreq => ({ type: types.CHANGE_ISREQ, isreq }),
    // 购物车列表
    changeCartList: arr => ({ type: types.CHANGE_CART_LIST, arr }),
    reqCartlist: () => (dispatch, getState) => {
        let uid = getState().login.info.uid
        //取原来Cartlist
        let checkedArr = [...getState().index.Cartlist]
        reqCartlist({ uid }).then(res => {
            if (res.data.code === 200) {
                //取list
                let list = res.data.list[1].content ? res.data.list[1].content : []
                //添加checked字段
                list.forEach((item) => {
                    //原来有--不变-------没有添加为false
                    item.checked = checkedArr.find((i) => i.id === item.id) ? checkedArr.find((i) => i.id === item.id).checked : false
                });
                //修改状态层 Cartlist
                dispatch(actions.changeCartList(list))
                // dispatch(actions.AllPrice())
                dispatch(actions.changeIsreq(true))
            }
        })
    },
    //购物车添加
    reqCartadd: (goodsid) => (dispatch, getState) => {
        let uid = getState().login.info.uid
        let { type, num } = getState().index
        reqCartadd({ uid, goodsid, type, num }).then(res => {
            if (res.data.list) {
                dispatch(actions.reqCartlist())
            }
        })
    },
    //购物车修改
    add: (id, num1) => (dispatch, getState) => {
        //自增
        num1 = num1 + 1
        //请求
        reqCartedit({ id, type: 2 }).then(res => {
            if (res.data.code === 200) {
                dispatch(actions.reqCartlist())
            }
        })
    },
    del: (id, num1) => (dispatch, getState) => {
        let { isreq } = getState().index
        console.log(isreq);

        if (num1 <= 1) {
            alert("宝贝不能再减少！！！");
            return;
        }
        if (isreq) {
            dispatch(actions.changeIsreq(false))
            reqCartedit({ id, type: 1 }).then(res => {
                if (res.data.code === 200) {
                    dispatch(actions.reqCartlist())
                }
            })
        }
    },
    changeAll: (e) => ({ type: types.CHANGE_ALL, e }),
    changeAlls: (e) => (dispatch, getState) => {
        dispatch(actions.changeAll(e))
        dispatch(actions.reqCartlist())
    },
    changeOne: (e, id) => (dispatch, getState) => {
        let { Cartlist } = getState().index
        //修改cartlist
        Cartlist.find(item => item.id === id).checked = e
        dispatch(actions.changeCartList(Cartlist))
        dispatch(actions.reqCartlist())
        // let { Cartlist } = getState().index
        let allChecked = Cartlist.every((item) => item.checked);
        dispatch(actions.changeAll(allChecked))
    },
    reqCartdel: (id) => (dispatch, getState) => {
        reqCartdel({ id }).then(res => {
            if (res.data.code == 200) {
                dispatch(actions.reqCartlist())
            }
        })
    },

}

//selectors 导出数据

export const getCartList = state => state.index.Cartlist
export const getAllChecked = state => state.index.allChecked
export const getPrice = state => state.index.price

export const getSum = state => {
    let { Cartlist } = state.index
    let AllPrice = Cartlist.reduce(
        (val, item) => (item.checked ? (val += item.price * item.num) : val),
        0
    );
    return AllPrice.toFixed(2)
}
export default reducer